<template>
	<view class="wrap">
		<view class="top"></view>
		<view class="content">
			<view class="title">注册</view>
			<input class="u-border-bottom"  v-model="name" placeholder="昵称" />
			<input class="u-border-bottom"  v-model="email" placeholder="邮箱" />
			<input class="u-border-bottom"  v-model="password" placeholder="密码" />
			<input class="u-border-bottom"  v-model="password_confirmation" placeholder="重复密码" />
			<button @tap="submit" :style="[inputStyle]" class="getCaptcha">注册</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: '',
				email: '',
				password: '',
				password_confirmation: '',
			}
		},
		computed: {
			inputStyle() {
				let style = {};
				if(uni.$u.test.email(this.email) && !uni.$u.test.isEmpty(this.name) && !uni.$u.test.isEmpty(this.password) && !uni.$u.test.isEmpty(this.password_confirmation)) {
					style.color = "#fff";
					style.backgroundColor = this.$u.color['warning'];
				}
				return style;
			}
		},
		methods: {
			async submit() {
				if(!uni.$u.test.email(this.email) || uni.$u.test.isEmpty(this.name) || uni.$u.test.isEmpty(this.password) || uni.$u.test.isEmpty(this.password_confirmation) || this.password !== this.password_confirmation) return;
				const params = {
					name: this.name,
					email: this.email,
					password: this.password,
					password_confirmation: this.password_confirmation
				}
				const registerRes =await uni.$u.api.authRegister(params);
				console.log(registerRes);
				if (registerRes.statusCode === 201){
					this.$u.toast('注册成功');
					setTimeout(()=>{
						uni.$u.route({
							type: 'reLaunch',
							url: 'pages/auth/login/login',
						});
					},1000);
				}else return false;
			}
		}
	}
</script>

<style lang="scss" scoped> //scoped防止样式污染
.wrap {
	font-size: 28rpx;
	.content {
		width: 600rpx;
		margin: 80rpx auto 0;
		
		.u-border-bottom{
			margin-top: 30rpx;
		}
		.title {
			text-align: center;
			font-size: 60rpx;
			font-weight: 500;
			margin-bottom: 100rpx;
		}
		input {
			text-align: left;
			margin-bottom: 10rpx;
			padding-bottom: 6rpx;
		}
		.tips {
			color: $u-info;
			margin-bottom: 60rpx;
			margin-top: 8rpx;
		}
		.getCaptcha {
			background-color: rgb(253, 243, 208);
			color: $u-tips-color;
			border: none;
			font-size: 30rpx;
			padding: 12rpx 0;
			margin-top: 30rpx;
			
			&::after {
				border: none;
			}
		}
	}
}
</style>
